home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #9 / Amiga Plus CD - 2004 - No. 09.iso / amigaplus / online / ibrowse_plugins / ibmht / mht.rexx < prev   
OS/2 REXX Batch file  |  2004-08-03  |  6KB  |  257 lines

  1. /*
  2.  * $VER: mht.rexx 1.01 (12.2.2003)
  3.  *
  4.  * ARexx script for IBrowse 1.2+
  5.  * Written by A Niven-Jenkins © 2002-2003 Cremlin Software
  6.  *
  7.  * Adds support for .mht files to IBrowse via an external viewer and YAM
  8.  *
  9.  * MIME Type: text/x-mht
  10.  * Extension: !mht
  11.  *    Action: External viewer
  12.  *    Viewer: SYS:RexxC/RX
  13.  * Arguments: <path>mht.rexx %f
  14.  *
  15.  */
  16.  
  17. PARSE ARG pmhtfile
  18. OPTIONS RESULTS
  19.  
  20. /* required libraries */
  21. CALL ADDLIB('rexxsupport.library', 0, -30, 0)
  22.  
  23. /* specify a temporary directory */
  24. ptempdir = 'T:'
  25.  
  26. /* specify the crlf stripper executable */
  27. pcrlf2lf = 'C:CRLF2LF'
  28.  
  29. /* specify the name of yams "deleted" folder
  30.    nb. it is NOT case sensitive */
  31. pdeleted = 'deleted'
  32.  
  33. /* .mht to mbox conversion */
  34. pmboxhead = ptempdir || 'head.mbox'
  35. pmboxbody = ptempdir || 'body.mbox'
  36. pmboxfile = ptempdir || 'mht.mbox'
  37. pmboxfrom = 'From xxx@xxx.xxx ddd mmm dd hh:nn:ss yyyy'
  38.  
  39. /* constants & variables */
  40. pclstr = 'Content-Location:'
  41. pmimetype = 'text/html'
  42. pprotocol = 'file://localhost/'
  43. pprotocollen = LENGTH(pprotocol)
  44. purlindex = -1
  45. pinfile = 'infile'
  46. poutfile = 'outfile'
  47. ptempfile = ptempdir || 'mhttemp'
  48. pdeletedelay = 500
  49. pfiles.count = 0
  50.  
  51. /* if yam isn't loaded, load it (iconified) */
  52. IF ~SHOW('Ports', 'YAM') THEN DO
  53.  ADDRESS COMMAND
  54.  'Run >NIL: YAM:YAM HIDE NOCHECK'
  55.  'SYS:RexxC/WaitForPort YAM'
  56.  
  57.  ADDRESS
  58.  ploadedyam = TRUE
  59. END
  60.  
  61. /* convert <pmhtfile> into "mbox" format */
  62. IF EXISTS(pmhtfile) THEN DO
  63.  ADDRESS COMMAND
  64.  'C:Echo ' || pmboxfrom || ' TO ' || pmboxhead
  65.  pcrlf2lf || ' >NIL: ' || pmhtfile || ' ' || pmboxbody
  66.  'C:Join ' || pmboxhead || ' ' || pmboxbody || ' TO ' || pmboxfile
  67.  
  68.  ADDRESS
  69.  IF EXISTS(pmboxhead) THEN DELETE(pmboxhead)
  70.  IF EXISTS(pmboxbody) THEN DELETE(pmboxbody)
  71. END
  72. ELSE
  73.  CALL SHUTDOWN
  74.  
  75. /* find and store any <pclstr> header fields within <pmboxfile>
  76.    nb. yam uses "Content-Dispostion: attachment; filename="xxxxx"" */
  77. IF OPEN(pinfile, pmboxfile) THEN DO
  78.  counter = 0
  79.  
  80.  DO UNTIL EOF(pinfile)
  81.   pinstr = READLN(pinfile)
  82.  
  83.   IF POS(pclstr, pinstr) > 0 THEN DO
  84.    pfiles.location.counter = STRIP(RIGHT(pinstr, LENGTH(pinstr) - LENGTH(pclstr)))
  85.    pfiles.filename.counter = PARSEFILENAME(pfiles.location.counter)
  86.    pfiles.filenamelen.counter = LENGTH(pfiles.filename.counter)
  87.    pfiles.saveas.counter = ptempdir || pfiles.filename.counter
  88.    pfiles.saveaslen.counter = LENGTH(pfiles.saveas.counter)
  89.    counter = counter + 1
  90.   END
  91.  END
  92.  
  93.  pfiles.count = counter
  94.  CLOSE(pinfile)
  95. END
  96. ELSE
  97.  CALL SHUTDOWN
  98.  
  99. /* import <pmboxfile> into yam and save any attachments */
  100. IF SHOW('Ports', 'YAM') THEN DO
  101.  ADDRESS YAM
  102.  
  103.  FOLDERINFO STEM pofi.
  104.  GETSELECTED STEM pogs.
  105.  
  106.  SETFOLDER pdeleted
  107.  GETSELECTED STEM pdgs.
  108.  
  109.  NEWMAILFILE STEM pnmf.
  110.  MAILIMPORT pmboxfile
  111.  SETMAILFILE pnmf.filename
  112.  
  113.  MAILREAD QUIET
  114.  READINFO STEM pri.
  115.  
  116.  DO counter = 0 FOR pri.filename.count
  117.   pfiles.filetype.counter = pri.filetype.counter
  118.   READSAVE counter + 1 FILENAME pfiles.saveas.counter OVERWRITE
  119.  END
  120.  
  121.  READCLOSE
  122.  MAILDELETE ATONCE FORCE
  123.  
  124.  IF pdgs.num.count > 0 THEN DO
  125.   SETMAIL pdgs.num.0
  126.   DO counter = 0 FOR pdgs.num.count
  127.    LISTSELECT pdgs.num.counter
  128.   END
  129.  END
  130.  
  131.  SETFOLDER pofi.name
  132.  
  133.  IF pogs.num.count > 0 THEN DO
  134.   SETMAIL pogs.num.0
  135.   DO counter = 0 FOR pogs.num.count
  136.    LISTSELECT pogs.num.counter
  137.   END
  138.  END
  139.  
  140.  ADDRESS
  141. END
  142. ELSE
  143.  CALL SHUTDOWN
  144.  
  145. /* replace any links within <pmimetype> type files with local equivalents
  146.    nb. the first occurrence will be taken as the main url */
  147. DO counter = 0 FOR pfiles.count
  148.  IF pfiles.filetype.counter = pmimetype THEN DO
  149.   IF purlindex = -1 THEN purlindex = counter
  150.  
  151.   CALL LOCALISELINKS(pfiles.saveas.counter)
  152.  END
  153. END
  154.  
  155. /* pass <pfiles.saveas.<purlindex>> to ibrowse */
  156. IF SHOW('Ports', 'IBROWSE') THEN DO
  157.  ADDRESS IBROWSE
  158.  GOTOURL pprotocol || pfiles.saveas.purlindex
  159.  
  160.  ADDRESS
  161.  DELAY(pdeletedelay)
  162. END
  163. ELSE
  164.  CALL SHUTDOWN
  165.  
  166. /* SHUTDOWN()
  167.    deletes temporary files etc. */
  168.  
  169. SHUTDOWN:
  170.  
  171. /* delete <ptempfile>
  172.    nb. will not exist unless <LOCALISELINKS()> failed */
  173. IF EXISTS(ptempfile) THEN DELETE(ptempfile)
  174.  
  175. /* delete <pfiles.saveas.n> */
  176. DO counter = 0 FOR pfiles.count
  177.  IF EXISTS(pfiles.saveas.counter) THEN DELETE(pfiles.saveas.counter)
  178. END
  179.  
  180. /* delete <pmboxfile> */
  181. IF EXISTS(pmboxfile) THEN DELETE(pmboxfile)
  182.  
  183. /* if yam was loaded via script, quit it */
  184. IF ploadedyam = TRUE THEN DO
  185.  ADDRESS YAM
  186.  QUIT FORCE
  187. END
  188.  
  189. /* end gracefully */
  190. EXIT 0
  191.  
  192. /* LOCALISELINKS()
  193.    replaces references to <pfiles.location.n>...<pfiles.filename.n> within
  194.    <pfile> with the equivalent <pfiles.saveas.n> */
  195.  
  196. LOCALISELINKS:
  197.  
  198. PARSE ARG pfile
  199.  
  200. IF RENAME(pfile, ptempfile) THEN DO
  201.  IF OPEN(pinfile, ptempfile) THEN DO
  202.   IF OPEN(poutfile, pfile, 'W') THEN DO
  203.  
  204.    DO UNTIL EOF(pinfile)
  205.     pinstr = READLN(pinfile)
  206.  
  207.     DO loop = 0 FOR pfiles.count
  208.      pstartpos = 0
  209.      pendpos = 1
  210.  
  211.      DO UNTIL pstartpos = 0
  212.       pstartpos = POS(pfiles.filename.loop, pinstr, pendpos)
  213.  
  214.       IF pstartpos > 0 THEN DO
  215.        ptempstr = LEFT(pinstr, pstartpos - 1)
  216.        ptemppos = MAX(LASTPOS(' ', ptempstr), LASTPOS('"', ptempstr), LASTPOS('=', ptempstr)) + 1
  217.  
  218.        IF ptemppos > 1 THEN DO
  219.         pinstr = INSERT(pprotocol || pfiles.saveas.loop, DELSTR(pinstr, ptemppos, (pstartpos - ptemppos) + pfiles.filenamelen.loop), ptemppos - 1)
  220.         pendpos = ptemppos + pprotocollen + pfiles.saveaslen.loop
  221.        END
  222.        ELSE
  223.         pendpos = pstartpos + pfiles.filenamelen.loop
  224.  
  225.       END
  226.      END
  227.     END
  228.  
  229.     WRITELN(poutfile, pinstr)
  230.    END
  231.  
  232.    CLOSE(poutfile)
  233.   END
  234.   ELSE
  235.    CALL SHUTDOWN
  236.  
  237.   CLOSE(pinfile)
  238.  END
  239.  ELSE
  240.   CALL SHUTDOWN
  241.  
  242.  IF EXISTS(ptempfile) THEN DELETE(ptempfile)
  243. END
  244. ELSE
  245.  CALL SHUTDOWN
  246.  
  247. RETURN
  248.  
  249. /* PARSEFILENAME()
  250.    returns the filename part of a fully qualified path */
  251.  
  252. PARSEFILENAME: PROCEDURE
  253.  
  254. PARSE ARG pfile
  255.  
  256. RETURN SUBSTR(pfile, MAX(LASTPOS(':', pfile), LASTPOS('/', pfile)) + 1)
  257.